﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using csvformater.csvReader;
using Path = System.IO.Path;

namespace csvformater.config
{
    /// <summary>
    /// ConfigurationWin.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigurationWin : Window
    {
        private string _configureFileSavedPath;
        private XmlDataProvider _configXmlDataProvider;

        private XmlDocument _xmlDocument;

        public string StrEncoding;

        public string StrFormat;


        public ConfigurationWin()
        {
            InitializeComponent();
            InitialData();
            setBinding();
        }

        /// <summary>
        /// the event will be dispatched when the cancel button has been click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// the event will be dispactched when then Ok button has been click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonOkClick(object sender, RoutedEventArgs e)
        {
            // model setting
            // column setting
            // code setting

            _configXmlDataProvider.Document.Save(_configureFileSavedPath);
            writeSchema();

            Close();

        }

        
        private void writeSchema()
        {
            var format = textBoxFormat.Text;
            var encoding = textBoxEncoding.Text;
            var path = Configure.Read("/Setting/Model/Path");
            NzCsvLoader.WriteSchema(path, format, encoding);

        }

        /// <summary>
        /// All data should be initialize by call this function
        /// </summary>
        private void InitialData()
        {
            _configureFileSavedPath = Configure.ConfigPath;
            _xmlDocument = new XmlDocument();
            _xmlDocument.Load(Configure.ConfigPath);
            _configXmlDataProvider = new XmlDataProvider { Document = _xmlDocument };
        }

        /// <summary>
        /// All bounding will be setted up by call this function
        /// </summary>
        private void setBinding()
        {
             
            /*
             * fileOpener -- binding data for csv model path
             */
            //_configXmlDataProvider.XPath = "/Setting//Model[@name='bonas']";
            fileOpener.filePath.DataContext = _configXmlDataProvider;
            fileOpener.filePath.SetBinding(TextBox.TextProperty, new Binding()
                                                                     {
                                                                         XPath = "/Setting/Model/Path"
                                                                     });
            /*
             * binding data for csv category type
             */
            comboBox_types.DataContext = _configXmlDataProvider;
            comboBox_types.SetBinding(ItemsControl.ItemsSourceProperty, new Binding()
                                                                            {
                                                                                XPath = "/Setting/Category/Type"
                                                                            });
            /*
             * binding data for csv from and to
             */
            ListBox_From.DataContext = _configXmlDataProvider;
            ListBox_From.SetBinding(ItemsControl.ItemsSourceProperty, new Binding()
                                                                            {
                                                                                XPath = "/Setting/Format"
                                                                            });
            ListBox_To.DataContext = _configXmlDataProvider;
            ListBox_To.SetBinding(ItemsControl.ItemsSourceProperty, new Binding()
                                                                        {
                                                                            XPath = "/Setting/Format"
                                                                        });

            /*
             * StrEncoding StrFormat
             */
            textBoxEncoding.DataContext = _configXmlDataProvider;
            textBoxEncoding.SetBinding(TextBox.TextProperty, new Binding()
                                                                 {
                                                                     XPath = "/Setting/Schema/line[@name='CharacterSet']"
                                                                 });
            textBoxFormat.DataContext = _configXmlDataProvider;
            textBoxFormat.SetBinding(TextBox.TextProperty, new Binding()
                                                               {
                                                                   XPath = "/Setting/Schema/line[@name='Format']"
                                                               });


        }
        /// <summary>
        /// when the ComboboxTypeChanged event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboboxTypeChanged(object sender, SelectionChangedEventArgs e)
        {
            /*
             * BOUNDING
             * textBox_categoryName
             * textBox_categoryCode
             */
            int index = comboBox_types.SelectedIndex + 1;
            if (comboBox_types.SelectedIndex >= 0)
            {
                textBox_categoryName.SetBinding(TextBox.TextProperty, new Binding()
                {
                    Source = _configXmlDataProvider,
                    XPath = String.Format("Setting/Category/Type[{0}]/@name", index)

                });

                textBox_categoryCode.SetBinding(TextBox.TextProperty, new Binding()
                {
                    Source = _configXmlDataProvider,
                    XPath = String.Format("Setting/Category/Type[{0}]/@code", index)
                });
            }
        }

        private void rdbSemicolon_Checked(object sender, RoutedEventArgs e)
        {
            textBoxFormat.Text = "Delimited(;)";
        }

        private void rdbTab_Checked(object sender, RoutedEventArgs e)
        {
            textBoxFormat.Text = "TabDelimited";
        }

        private void rdbSeparatorOther_Checked(object sender, RoutedEventArgs e)
        {
            textBoxFormat.Text = "Delimited(,)";
        }

        private void rdbAnsi_Checked(object sender, RoutedEventArgs e)
        {
            textBoxEncoding.Text = "ANSI";
        }


        private void rdbOEM_Checked(object sender, RoutedEventArgs e)
        {
            textBoxEncoding.Text = "OEM";
        }

        private void textBoxFormat_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxFormat.Text == "Delimited(;)")
                rdbSemicolon.IsChecked = true;
            else if (textBoxFormat.Text == "Delimited(,)")
                rdbSeparatorOther.IsChecked = true;
            else if (textBoxFormat.Text == "TabDelimited")
                rdbTab.IsChecked = true;
        }

        private void textBoxEncoding_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxEncoding.Text == "ANSI")
                rdbAnsi.IsChecked = true;
            else if (textBoxEncoding.Text == "OEM")
                rdbOEM.IsChecked = true;
        }
    }
}
    
